
import java.util.Arrays;
public class Test7 {
    public static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            boolean flg = false;
            //i代表趟数
            //j代表每一趟比较的次数
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j+1]){
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                    flg = true;
                }
                if(flg==false){
                    break;      //说明有序了
                }
            }

        }
    }
    public static void main(String[] args) {
        int[] arr3 = {1,5,2,4,3,6};
        bubbleSort(arr3);
        System.out.println(Arrays.toString(arr3));
    }
    public static void main12(String[] args) {
        int[] arr = new int[10];
        Arrays.fill(arr,100);//将100填充进arr里
        System.out.println(Arrays.toString(arr));
        Arrays.fill(arr,1,4,1);//在[1,4)位置里填充1
    }
    public static void main11(String[] args) {
        int[] arr3 = {1,5,2,4,3,6};
        int[] arr4 = {1,5,2,4,3,6};
        System.out.println(Arrays.equals(arr3, arr4));//比较两个数组的对应位置是否相同
    }
    public static int binarySearch(int[] arr,int x){
        int right = arr.length - 1;
        int left = 0;
        while(left <= right){
            int mid = (left + right) / 2;
            if(x > arr[mid]){
                left = mid + 1;
            }
            else if(x < arr[mid]){
                right = mid - 1;
            }
            else{//找到返回下标
                return mid;
            }
        }
        return -1;
    }
    public static void main10(String[] args) {
        //二分查找
        int[] arr3 = {1,5,2,4,3,6};
        Arrays.sort(arr3);//对数组进行排序
        System.out.println(Arrays.binarySearch(arr3, 4));//使用工具完成二分查找
// System.out.println(Arrays.toString(arr3));
       // System.out.println(binarySearch(arr3, 4));
    }
    public static int findVal(int[] arr1,int x){
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i]==x){
                return i;
            }
        }return -1;
    }
    public static void main7(String[] args) {
        //遍历数组查找元素的值
        int[] arr = {1,2,3,4,5,6};
        System.out.println(findVal(arr, 4));
    }
    public static void main6(String[] args) {
        //使用工具类进行数组的拷贝
        int[] arr1 = {1,2,3,4,5,6};
        int[] arr2 = new int[arr1.length * 2];//扩容
        /*int[]arr2 = Arrays.copyOf(arr1,arr1.length);
        System.out.println(Arrays.toString(arr2));*/
        System.arraycopy(arr1,0,arr2,0,arr1.length);//原数组，原数组开始拷贝的位置，目的数组，目的数组的哪个位置，拷贝的长度
        System.out.println(Arrays.toString(arr2));
        //数组扩容
        int[]arr3 = Arrays.copyOf(arr1,arr1.length * 2);
        System.out.println(Arrays.toString(arr3));//123456000000
        //拷贝下标[1,4）的元素
        //若超出范围，则能拷贝多少拷贝多少,后面补0
        int[]arr4 = Arrays.copyOfRange(arr1,1,4);
        System.out.println(Arrays.toString(arr4));
    }
    public static void main5(String[] args) {
        //拷贝数组
        int[] arr1 = {1,2,3,4,5,6};
        int[] arr2 = new int[arr1.length];
        for (int i = 0; i < arr1.length; i++) {
            arr2 = arr1;
        }
        System.out.println(Arrays.toString(arr2));
    }
    public static String myToString(int[] arr){
        //自己写一个方法实现toString
        String ret = "[";
        for (int i = 0; i < arr.length; i++) {
            ret += arr[i];
            if(i != arr.length - 1){
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
    public static void main4(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(myToString(arr));
    }
    public static int[] fac3(){
       /* int[] arr = {1,2,3,4};
        return arr;*/
        return new int[]{1,2,3,4,5,6};
    }
    public static void main3(String[] args) {
        int[] ret = fac3();
        //借助工具类将数组的返回值变为字符串形式
      /*  String S = Arrays.toString(ret);//ctrl + 鼠标点击就可以跳转到方法源码
        System.out.println(S);*/
        System.out.println(Arrays.toString(ret));
      /*  for (int i = 0; i < ret.length; i++) {
            System.out.print(ret[i] + " ");
        }*/
    }
    public static void fac1(int[] array){
         array = new int[10];           //形参数组指向一个新的对象，并不影响实参的指向
        //array = null;
    }
    public static void fac2(int[] array){
        array[0] = 99;          //都指向同一对象，因此会改变array2[0]的值
    }
    public static void main2(String[] args) {
        int [] array1 = {1,2,3,4};
        int [] array2 = {1,2,3,4};
        fac1(array1);
        for (int i = 0; i < array1.length; i++) {
            System.out.print(array1[i] + " ");//1 2 3 4
        }
        System.out.println();
        fac2(array2);
        for (int i = 0; i < array2.length; i++) {
            System.out.print(array2[i] + " ");//99 2 3 4
        }
    }
    public static void main1(String[] args) {
            int[] arr1 = {1,2,3,4};
            int[] arr2 = null;      //arr2引用指向空对象
            arr2 = arr1;            //将arr1这个引用指向arr2这个引用指向的对象
            arr2[0] = 100;          //两个应用都可以修改这个对象的值
            arr2[1] = 200;
            for(int x : arr1){          //使用for else遍历数组
                System.out.println(x + " ");
            }
    }
}
